<?php
class login extends Controller{
	private $jsonarr = array();
	function index(){
	   if($this->user->isLogged()){
			$this->response->redirect(MAIN_PAGE."search");
		}
		$this->document->setTitle("Document Management Solutions");
		$this->children=array("header","footer");
		$this->template="login.html";
		$this->response->setOutput($this->render(TRUE),0);
	}

	function loginpost(){
		if($this->request->is_ajax_request()){
			if($this->validateForm()){
				$this->jsonarr['status']=true;
			}
		}
		echo json_encode($this->jsonarr);
	}

	private function validateForm(){
		if(strlen(utf8_decode($this->request->post['uname']))==0){
			$this->jsonarr['msg']="Username should not be empty";
			$this->jsonarr['status']=false;
			return false;
		}
		if(strlen(utf8_decode($this->request->post['psw']))==0){
			$this->jsonarr['msg']="Password should not be empty";
			$this->jsonarr['status']=false;
			return false;
		}
		if(!$this->user->login($this->request->post['uname'], $this->request->post['psw']))
		{
			$this->jsonarr['msg']="No match for Username and/or Password.";
			$this->jsonarr['status']=false;
			return false;
		}
		return true;
	}

}